	subroutine input_opt(ftol,xtol,gtol,maxfev,numpar,
     &                 NRC,metodo,mode,nprint,factor,epsfcn,irefin)
        include 'param_dim.inc'	
	COMMON /cFDAT/ FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES
	COMMON /incdata/ C0,CINC,NNINC,NUMPI,NUMPCI,NINCL,VARIA	
	CHARACTER*15 FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES
	integer FOPT,FDAT,f_new
	
	double precision p(npar_max),s(npar_max)
	double precision ftol,xtol,gtol
	integer maxfev,numcc,numpar,NRC,mode,metodo,nprint
	double precision factor,epsfcn
	double precision PGLIM(nparg_max,3),CINC(ninc_max),C0
	integer VARIA(ninc_max)	
	integer NNINC(ninc_max),NUMPI(ninc_max),NUMPCI(ninc_max)
	common/geom/p,s,numpc


	if(irefin .eq. 1)then
	FILE_DAT='bacia_ext00.dat'
	
	WRITE(*,*) 'Arquivo cont.ext.: bacia_ext00.dat ? (0=>NAO)'
	READ(*,*) ISIM
	
	IF(ISIM.EQ.0) THEN
            WRITE(*,'(a/)')' Entre arq.DADOS GEOM.FIXOS(10 char.bas.):'
            READ(*,'(A)') FILE_DAT
	END IF
	
	WRITE(*,'(a/)')' Arq.. MEDIDAS NOS ELETRODOS (10 char. bas.):'
	READ(*,'(A)') FILE_DEX
	
	write(*,*) 'Entre com ARQUIVO DE DADOS DE OTIMIZACAO:'
	READ(*,*) FILE_OPT


	OPEN(4,file=file_opt//'.out',status='new')

!  SAIDA 
	WRITE(4,*)'Arq.DADOS GEOM.FIXOS(10 char.bas.):'
	WRITE(4,*) FILE_DAT

	WRITE(4,*)'Arq.. MEDIDAS NOS ELETRODOS (10 char. bas.):'
	WRITE(4,*) FILE_DEX
	
	WRITE(4,*) 'ARQUIVO DE DADOS DE OTIMIZACAO:'
	WRITE(4,*) FILE_OPT
! 
	
	FOPT= 11
	OPEN (FOPT,FILE=FILE_OPT,STATUS='OLD')


	READ(FOPT,*) ftol,xtol,gtol,maxfev
	


	
	write(4,*)
	write(4,*)'   Parametros do alg. de otimizacao'
	write(4,*)
	write(4,*)'   ftol: ',ftol
	write(4,*)'   xtol: ',xtol
	write(4,*)'   gtol: ',gtol
	write(4,*)'   maxfev: ',maxfev

	READ(FOPT,*) metodo
	if(metodo .eq. 1)then
		READ(FOPT,*) mode,factor,nprint,epsfcn
		write(4,*)'   mode: ',mode
		write(4,*)'   factor: ',factor
		write(4,*)'   nprint: ',nprint
		write(4,*)'   epsfcn: ',epsfcn
	else if(metodo .eq. 2)then
		READ(FOPT,*) mode,factor,nprint
		write(4,*)'   mode: ',mode
		write(4,*)'   factor: ',factor
		write(4,*)'   nprint: ',nprint
	endif

	READ(FOPT,*) NINCL


	write(4,1020) NINCL
 1020   format( //,3x,'Numero de inclusoes: ',i5,/)

	NUMPAR = 0
	NUMPC = 0
	do i=1,nincl
		read(fopt,*)NUMPCI(i),NUMPI(i),cinc(i),VARIA(i)
		write(4,*)'   N.PCs: ',NUMPCI(i),'  N.Params: ',NUMPI(i),
     &			'  Condut.: ',cinc(i),'  Varia?: ',VARIA(i)
		NUMPAR = NUMPAR + NUMPI(i)
		NUMPC = NUMPC + NUMPCI(i)
	enddo

	if(NUMPAR .NE. NUMPC .and. 
     &		NUMPAR .NE. NUMPC*2 .and. 
     &		NUMPAR .NE. NUMPC*3)then
		write(*,*) 'ERRO: num. de param. ou pontos de controle'
		write(4,*) 'ERRO: num. de param. ou pontos de controle'
		stop
	endif

	write(4,*) 
	if(NUMPAR .EQ. NUMPC)then
		write(4,*) 'variaveis de otimizacao: parametro S'
	else if(NUMPAR .EQ. 2* NUMPC)then
		write(4,*) 'variaveis de otimizacao: coordenadas'
	else if(NUMPAR .EQ. 3*NUMPC)then
		write(4,*) 'variaveis de otimizacao: coord. e param. S'
	endif
		write(4,*)

	i=0
	do iinc = 1, nincl
		do ipi= 1, NUMPCI(iinc)
			i=i+2
			READ(FOPT,*) P(i-1),P(i),S(i/2)
		enddo
	enddo

	WRITE(4,*) ' Pontos de Controle Iniciais'
	WRITE(4,*) ' inclusao       X          Y'

	i=0
	do iinc = 1, nincl
		do ipi= 1, NUMPCI(iinc)
			i=i+2
			write(4,1000)iinc,P(i-1),P(i),S(i/2)	
		enddo
	enddo
 1000   FORMAT(I5,3F15.4)


	FDAT = 22
	open(FDAT,file = FILE_DAT)
	READ(FDAT,150)TITULO
  150 	FORMAT(18A4)
	READ(FDAT,*)NFIX,NEPE,NCasos,ITCC, VCC
	NRC = Ncasos*(16-3)

	close(FDAT)
	else !se irefin=2,3,4....
	
		f_new=10
		open(f_new,file='auxiliar_new.tmp')
		numpc=0
		i=0
		do iinc = 1, nincl
		    read(f_new,*)NUMPCI(iinc)
		    numpc=numpc+NUMPCI(iinc)
		    do ipi= 1, NUMPCI(iinc)
			    i=i+2
			    READ(f_new,*) P(i-1),P(i),S(i/2)
		    enddo
		enddo
		close(f_new)
		call system('rm auxiliar_new.tmp')

		numpar = 2*numpc  !refinamento sempre com otimizacao de coordenadas
		if(NINCL.ne.1)then
		    write(*,*) 'refin. c + de 1 inclusao ainda nao implementado'
		    stop
		endif
		
	endif
	
	return
	end


